$(function(){
	var userGrid = $("#followup");
	var reamk = $("#tags").val();
	var i=0;
	//初始化
	userGrid.datagrid({
		url:'/followup/list',
		fit:true,
		fitColumns:true,
		border:false,
		rownumbers:true,
		pagination : true,// 是否显示分页栏
		onLoadSuccess:function(data){  
		    $(".note").tooltip({
		    	 onShow: function(){
		    		 $(this).tooltip('tip').css({
		                 width:'230',
	                     boxShadow: '1px 1px 3px #292929'
		    		 });
		    	 }
		    });  
		}, 
		columns : [ [ {
			field : 'student',
			title : '姓名',
			align : 'center',
			width : 90,
			formatter : function(val,rec){
				return rec.student.name;
			}
		}, {
			field : 'emp',
			title : '跟进人',
			align : 'center',
			width : 90,
			formatter : function(val,rec){
				return rec.emp.name;
			}
		}, {
			field : 'grade',
			title : '跟进等级',
			align : 'center',
			width : 90,
			formatter : function(val,rec){
				return rec.grade.grade;
			}
		}, {
			field : 'followTime',
			title : '跟进时间',
			align : 'center',
			width : 90,
			formatter : function(val){
				var newTime = new Date(val);
				var year = newTime.getFullYear();  //获取年
		        var month = newTime.getMonth() + 1;    //获取月
		        var day = newTime.getDate(); //获取日
		        var hours = newTime.getHours(); 
		        var minutes = newTime.getMinutes();
		        var time = year + "-" + month + "-" + day;
				return time;
			} 
		} , {
			field : 'nextFollowTime',
			title : '下次跟进时间',
			align : 'center',
			width : 90
		} , {
			field : 'followContent',
			title : '跟进内容',
			align : 'left',
			width : 90,
			formatter: function(value, row, index) {
                var abValue = value;
                if (value.length>=22) {
                    abValue = value.substring(0,19) + "...";
                }
                var content = '<a href="#" title="' + value + '" class="note">' + abValue + '</a>';
                return content;
            }
		} , {
			field : 'mode',
			title : '跟进方式',
			align : 'center',
			width : 90
		}, {
			field : 'remark',
			title : '批阅内容',
			align : 'center',
			width : 90,
			formatter : function(value, row, index) {
				var bts = [];
				if(value==null ){
					bts.push('<a style="margin:0 auto;color:white;border-radius:15px;width:60px;height:20px;line-height:20px;background-color:#CC3333" data-id="' + row.id + '" data-index="'
							+ index + '" class="ctr py">未批阅</a>');
				}else if(value!=null){
				bts.push('<a data-id="' + row.id + '" data-index="'
						+ index + '" class="ctr py">查看</a>');
				}
				return bts.join('');
			},
			/*hidden: function(){
				if (reamk == '部门经理'){
					return false;
					// the function can return predefined css class and inline style
					// return {class:'c1',style:'color:red'}
				}else{
					return false;
				}
			}*/
		}, {
			field : 'ac',
			title : '操作',
			width : 100,
			align : 'center',
			formatter:function(value,row,index){
				var bts = [];
				bts.push('<a data-id="'+row.id+'" data-index="'+index+'" class="ctr delete">删除</a>');
				return bts.join('');
			}
		} ] ]
	});
	var name = $("#student");
	
	//检索按钮时间
	$("#userSearch").on("click",function(){
		userGrid.datagrid("load",{
			student:name.val(),
		});
	});
	var form = $("#userSearchForm");

	$("#userReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		userGrid.datagrid("load", {});
	});
	var gridPanel = userGrid.datagrid("getPanel");
	gridPanel.on("click", "a.py", function() {
		var index = this.dataset.index;
		var rows = userGrid.datagrid("getRows");
		var id = rows[index].id;
		piyue(id);
	}).on("click", "a.delete", function() {
		// 获取data-xxx绑定的标签属性数据
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要删除此数据?", function(r) {
			// 点击了确定按钮，r返回的结果为true
			if (r) {
				$.get("followup/delete/" + id, function(rs) {
					if (rs.success) {
						// 刷新表格
						userGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	});
	
	function piyue(id){
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href : 'followup/piyue' + (id ? ("/" + id) : ""),
			title:"批阅信息",
			height:250,
			width:400,
			modal : true,
			onClose : function() {
				// 窗口关闭的同时销毁此窗口
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					// 获取到表单对象
					var piyueForm = $("#piyueForm");
					// 校验表单
					if (piyueForm.form('validate')) {
						// serialize方法可以获取到表单所有数据
						$.post("followup/piyuesave", piyueForm.serialize(), function(rs) {
							if (rs.success) {
								//刷新数据表格
								userGrid.datagrid("reload");
								//关闭窗口
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}
				}
			} ]
		});
	}
	
});




$.extend($.fn.datagrid.methods, {  
    tooltip : function (jq, fields) {  
        return jq.each(function () {  
            var panel = $(this).datagrid('getPanel');  
            if (fields && typeof fields == 'object' && fields.sort) {  
                $.each(fields, function () {  
                    var field = this;  
                    bindEvent($('.datagrid-body td[field=' + field + '] .datagrid-cell', panel));  
                });  
            } else {  
                bindEvent($(".datagrid-body .datagrid-cell", panel));  
            }  
        });  
  
        function bindEvent(jqs) {  
            jqs.mouseover(function () {  
                var content = $(this).text();  
                if(content && content!=""){  
                    $(this).tooltip({  
                        content : content,  
                        trackMouse : true,  
                        deltaX: 15,  
                        deltaY: -5,  
                        onShow: function(){  
                            $(this).tooltip('tip').css({  
                                backgroundColor: '#f7f5d1',  
                                borderColor: 'black'  
                            });  
                        }  
                    }).tooltip('show');  
                }  
            });  
        }  
    }  
});  
